# 导入Flask类库
import json

from flask import Flask, request, Response


from gevent import pywsgi

import corrector

# 创建应用实例
app = Flask(__name__)


# 视图函数（路由）
@app.route("/")
def hello_world():
    return "Hello, World!"


@app.route("/correct_text", methods=["POST"])
def correct_text():
    # 在调用thread之前打印当前线程信息
    """接受前端传送过来的文件"""
    texts = request.get_json()
    results = corrector.correct_texts(texts)
    result = {"code": 0, "result": results}

    return Response(json.dumps(result, ensure_ascii=False), mimetype="application/json")




# 启动服务
if __name__ == '__main__':
    # threaded=False 使用单线程模式，处理完一个请求再处理下一个请求
    # app.run(host='0.0.0.0', port=9012, threaded=False, debug=False)

    app.config['JSON_AS_ASCII'] = False
    app.config['threaded'] = False
    app.config['debug'] = False
    port = 8712
    # app.run(host='0.0.0.0', port, threaded=True, debug=False)
    print("端口:", port)
    server = pywsgi.WSGIServer(('0.0.0.0', port), app)
    server.serve_forever()
